package com.readbook.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.codec.digest.DigestUtils;
//import org.apache.commons.codec.digest.DigestUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class getthirdInfo {


    /**
     * @author 牟明艳
     * @comments 远程获取用户基本信息
     * @time 2019年8月17日12:13:04
     */
    public static Map<String,Object>getUserInfoThird(String dlzh,String dlma)
    {
        Map<String, Object> remap = new HashMap<String, Object>();
        Map<String, Object> mapParam = new HashMap<String, Object>();
        mapParam.put("username", dlzh);
        mapParam.put("password",dlma);
        mapParam.put("user_type",1);//1学生 2教师
        String pathUrl = "https://gyhx-api.86358.com/third/getAccessToken";
        String result = HttpPostUrl.sendPost(pathUrl, mapParam);
        result=HttpPostUrl.decodeUnicode(result);
        Map<String,Object>resultmap= JSON.parseObject(result);

        if(resultmap.get("code").toString().equals("1000001")){
            //用户名不存在 看看是否为教师账号
            mapParam.remove("user_type");//1学生 2教师
            mapParam.put("user_type",2);//1学生 2教师
            result = HttpPostUrl.sendPost(pathUrl, mapParam);
            result=HttpPostUrl.decodeUnicode(result);
            Map<String,Object>resultmap1=JSON.parseObject(result);
            if(resultmap1.get("code").toString().equals("1000001")){
                //即不是学生账号 也不是教师账号
                remap.put("msg","账号不存在!");
                remap.put("result",false);
            }else if(resultmap1.get("code").toString().equals("1000002")){
                //密码错误
                remap.put("msg","密码错误,请重试!");
                remap.put("result",false);
            }else{
                //成功 账号为教师
                String tokendata=resultmap1.get("data").toString();
                Map<String,Object> tokenmaps=JSON.parseObject(tokendata);
                String token=tokenmaps.get("token").toString();

                String teaurl="https://gyhx-api.86358.com/third/teacherInfo";
                String tearesult=HttpPostUrl.httpURLConectionGET(teaurl,token);

                Map<String,Object>teamaps=JSON.parseObject(tearesult);
                if(!teamaps.get("code").toString().equals("0")){
                    remap.put("msg",teamaps.get("msg").toString());
                    remap.put("result",false);
                }else{
                    String teadatas=teamaps.get("data").toString();
                    Map<String,Object> teadatamap=JSON.parseObject(teadatas);
                    remap.put("msg","登录成功!");
                    remap.put("result",true);
                    remap.put("name",teadatamap.get("name"));
                    remap.put("sex",teadatamap.get("sex"));
                    remap.put("identity",teadatamap.get("identity_name"));
                    remap.put("user_type",2);
                    if(teadatamap.get("banji_name")!=null && !teadatamap.get("banji_name").toString().equals("")){
                        String banji_name=teadatamap.get("banji_name").toString();
                        String darr[]= banji_name.split("年级");
                        String nj=getXxNUM(darr[0]);
                        String bj=darr[1].replaceAll("班","");
                        remap.put("nj",nj);
                        remap.put("bj",bj);
                    }
                }

            }
        }else if(resultmap.get("code").toString().equals("1000002")){
            //密码错误
            remap.put("msg","密码错误,请重试!");
            remap.put("result",false);
        }else{
            //成功 账号为学生
            String tokendata=resultmap.get("data").toString();
            Map<String,Object> tokenmaps=JSON.parseObject(tokendata);
            String token=tokenmaps.get("token").toString();
            String stuurl = "https://gyhx-api.86358.com/third/studentInfo";
            String sturesult=HttpPostUrl.httpURLConectionGET(stuurl,token);

            Map<String,Object>stumaps=JSON.parseObject(sturesult);
            if(!stumaps.get("code").toString().equals("0")){
                remap.put("msg",stumaps.get("msg").toString());
                remap.put("result",false);
            }else{
                remap.put("msg","登录成功!");
                remap.put("result",true);
                remap.put("user_type",1);
                String studatas=stumaps.get("data").toString();
                List<Map<String,Object>> liststu = (List<Map<String,Object>>) JSONArray.parse(studatas);
                remap.put("stusize",liststu.size());
                if(liststu.size()==1){
                    remap.put("name",liststu.get(0).get("name"));
                    remap.put("sex",liststu.get(0).get("sex"));
                    remap.put("phone",liststu.get(0).get("phone"));
                    remap.put("identity",liststu.get(0).get("member_post_name"));
                    remap.put("bj",liststu.get(0).get("banji_num"));
                    String ruxn=liststu.get(0).get("year").toString();
                    remap.put("year",ruxn);
                    Calendar cal = Calendar.getInstance();
                    int year = cal.get(Calendar.YEAR);
                    int month = cal.get(Calendar.MONTH )+1;
                    int nj=year-Integer.parseInt(ruxn);
                    if(month>=8){
                        nj=nj+1;//因为上半学期还没有升学
                    }
                    remap.put("nj",nj);
                }else{
                    List<Map<String,Object>> listmaps=new ArrayList<>();
                    for(int m=0;m<liststu.size();m++){
                        Map<String,Object> struformap=new HashMap<>();
                        struformap.put("name",liststu.get(m).get("name"));
                        struformap.put("sex",liststu.get(m).get("sex"));
                        struformap.put("phone",liststu.get(m).get("phone"));
                        struformap.put("identity",liststu.get(m).get("member_post_name"));
                        struformap.put("bj",liststu.get(m).get("banji_num"));
                        String ruxn=liststu.get(m).get("year").toString();
                        remap.put("year",ruxn);
                        Calendar cal = Calendar.getInstance();
                        int year = cal.get(Calendar.YEAR);
                        int month = cal.get(Calendar.MONTH )+1;
                        int nj=year-Integer.parseInt(ruxn);
                        if(month>=8){
                            nj=nj+1;//因为上半学期还没有升学
                        }
                        struformap.put("nj",nj);
                        listmaps.add(struformap);
                    }
                    remap.put("stus",listmaps);
                }
            }
        }
        return remap;
    }



    public static String getXxNUM(String dxnum){
        String numarr[]={"零","一","二","三","四","五","六","七","八","九"};
        String xinum="";
        for(int j=0;j<numarr.length;j++){
            if(numarr[j]==dxnum){
                xinum=j+"";
            }
        }
        return xinum;
    }

    /**
     * @author 牟明艳
     * @comments 获取所有学生信息
     * @time 2019年12月10日10:32:18
     */
    public static Map<String,Object> getMemberList(String last_time)
    {
        Map<String,Object>rmap=new HashMap<>();
        Map<String,Object>resultmap= getStuList(last_time);
        if(resultmap.get("code").toString().equals("0")){
            String datas=resultmap.get("data").toString();
            Map<String,Object> datamap=JSON.parseObject(datas);
            //最后更新的时间
            String lasttime=datamap.get("time").toString();
            String studatas=datamap.get("list").toString();

            rmap.put("lasttime",lasttime);
            rmap.put("studatas",studatas);
            return rmap;
        }else{
            return new HashMap<>();
        }
    }


    /**
     * @author 牟明艳
     * @comments 获取所有学生信息
     * @time 2019年12月10日10:32:18
     */
    public static Map<String,Object> getStuList(String last_time)
    {
        String token="huanxiread2019";
        long timestamp=System.currentTimeMillis();
        int nonce=getsixRandom();
        String signature=sortAndEncrypt(token,timestamp+"",nonce+"");

        String pathUrl = "https://gyhx-api.86358.com/third/getMemberList?signature="+signature+"&timestamp="+timestamp+"&nonce="+nonce+"&last_time="+last_time;
        String result = HttpPostUrl.httpURLConectionGET1(pathUrl);

        result=HttpPostUrl.decodeUnicode(result);
        Map<String,Object>resultmap= JSON.parseObject(result);
        return resultmap;
    }




    /**
     * @author 牟明艳
     * @comments 获取所有教师信息
     * @time 2019年12月10日10:32:18
     */
    public static Map<String,Object> getteaList(long last_time)
    {
        Map<String,Object>rmap=new HashMap<>();
        Map<String,Object>resultmap= getTeacherList(last_time);
        if(resultmap.get("code").toString().equals("0")){
            String datas=resultmap.get("data").toString();
            Map<String,Object> datamap=JSON.parseObject(datas);
            String teadatas=datamap.get("list").toString();
            String lasttime=datamap.get("time").toString();
            rmap.put("lasttime",lasttime);
            rmap.put("teadatas",teadatas);
            return rmap;
        }else{
            return new HashMap<>();
        }
    }


    /**
     * @author 牟明艳
     * @comments 获取所有教师信息
     * @time 2019年12月10日10:32:18
     */
    public static Map<String,Object> getTeacherList(long last_time)
    {
        String token="huanxiread2019";
        long timestamp=System.currentTimeMillis();
        int nonce=getsixRandom();
        String signature=sortAndEncrypt(token,timestamp+"",nonce+"");

        Map<String, Object> mapParam = new HashMap<String, Object>();
        mapParam.put("signature", signature);
        mapParam.put("timestamp",timestamp);
        mapParam.put("nonce",nonce);
        mapParam.put("last_time",last_time);
        String pathUrl = "https://gyhx-api.86358.com/third/getTeacherList?signature="+signature+"&timestamp="+timestamp+"&nonce="+nonce+"&last_time="+last_time;
        String result = HttpPostUrl.httpURLConectionGET1(pathUrl);

        result=HttpPostUrl.decodeUnicode(result);
        Map<String,Object>resultmap= JSON.parseObject(result);
        return resultmap;
    }


    public static int getsixRandom(){
        int flag = new Random().nextInt(999999);
        if (flag < 100000)
        {
            flag += 100000;
        }
        return flag;
    }

    public static String sortAndEncrypt(String token, String timestamp, String nonce) {
        ArrayList<String> list = new ArrayList<String>();
        list.add(token);
        list.add(timestamp);
        list.add(nonce);
        Collections.sort(list);
        return DigestUtils.shaHex(list.get(0) + list.get(1) + list.get(2));
    }




    public static void main(String args[]) throws Exception {
        Map<String, Object> mapParam = new HashMap<String, Object>();
        //mapParam=getUserInfoThird("13985043032","123456");

        String lasttime="1577890466000";
        mapParam=getStuList(lasttime);
        //13985043032 15285997705
        System.out.println(mapParam);
    }


}
